/**
 * Created by szc on 2016/12/14.
 */
declare function
socketSend(json: any, fn: any);
declare function
serverDispatch(action: any, postData: any, fn: any);
declare function
sleep(s: any);
class TeskGun {
	private context: GunDeal;
	private tdata;
	private sign:boolean;
	public constructor(context: GunDeal) {
		this.context = context;
		var sdata = this.getGunType();
		this.startGun();
		this.sendServer(sdata);
	}
	/**
	 * 判断请求类型,并且生成相应的数据包，如果有金钱的减少，就减去相关的钱数
	 */
	private getGunType(): any {
		if (!this.context.ishasFree()) {
			var ok = this.context.context.crystalPanle.getCuMultiple() * this.context.context.crystalPanle.getCuZhu()*25;
			this.context.context.leftPanle.setMoney(this.context.context.leftPanle.getMoney() - ok);
			return this.normalRotateData();
		} else {
			this.context.context.freePanle.userFree();
			return this.freeRotateData();
		}
	}
	/**
	 * 免费游戏转动一次
	 */
	private freeRotateData(): any {
		this.sign = false;
		return {
			type: "freeslot",
			debugMode: "None"
		};
	}
    /**
     * 普通滚动
     */
	private normalRotateData(): any {
		this.sign = true;
		var point = this.context;
		return {
			type: "slot",
			Bet: 1,
			Denom: point.context.crystalPanle.getCuMultiple() * point.context.crystalPanle.getCuZhu() * 10000,
			Line: 25
		};
	}

	/**
	 * 请求服务器，相关的参数
	 */
	private sendServer(d: any): any {
		var point = this;
		serverDispatch('Play', d, function (data) {
//			console.log("游戏的回传参数", data);
			var postData = data.Value.SpinResult.Main.ReelResults;   //要判断c1的结果

			point.tdata = data;
			point.context.context.gunPanle.setData(data);
		});
	}

	/**
	 * 开始滚动并且隐藏水晶版
	 */
	private startGun(): void {
		this.context.context.crystalPanle.set_win(0);
		this.context.context.crystalPanle.displayDisPanle();
		this.context.context.gunPanle.start(this);
		this.context.context.soundctr.openGunSound();
	}

	/**
	 * 当滚动结束，回调函数
	 */
	private completeRotate(): void {
//		console.log("滚动结束");
		this.context.context.soundctr.closeGunSound();
		//判断赢得结果
		var data = this.tdata;
		// console.log("当前的结果",data);
		var winRe = data.Value.SpinResult.Main.WinResults[0];
		if (winRe) {
//			console.log("赢了");
			this.winScence();
		} else {
//			console.log("输了");
			this.completeSence();
			this.context.complete(); //告诉前台滚动完成
		}
	}

	/**
     * 整体完成以后的数据判断
     */
	private winScence(): void {
        /**
         * 获取完成后的连线数据
         */
		var data = this.tdata;
		var winRe = data.Value.SpinResult.Main.WinResults[0];
//		console.log("赢得的结果", winRe.Positions);
		if (winRe) {
			var reArr = [];
			reArr[0] = [];
			reArr[1] = [];
			reArr[2] = [];
			reArr[3] = [];
			reArr[4] = [];
			for (var i = 0; i < winRe.Positions.length; i++) {
				var one = winRe.Positions[i];
				reArr[one.X].push(one.Y);
			}
//			console.log("要渲染的结果", reArr);
//			console.log("渲染的图标", data.Value.SpinResult.Main.ReelSymbols)
			var point = this;
			window.setTimeout(function () {
				point.context.context.gunPanle.openAm(reArr, data.Value.SpinResult.Main.ReelSymbols);
				point.context.context.crystalPanle.changezhu();
				point.context.context.crystalPanle.set_zhu_win(data.Value.SpinResult.Main.TotalBet / 10000, data.Value.SpinResult.Main.TotalWin  / 10000);
				point.context.context.soundctr.playSound("small_coin_mp3");
				point.completeSence();
			}, 1000);

			window.setTimeout(function () {
				point.context.context.gunPanle.closeAm();
				point.inpectResult();
			}, 3500);

			// sleep(2000);
		}

	}

	private inpectResult() {
		var data = this.tdata;
		if (data.Value.SpinResult.Actions) {
			var freeData = data.Value.SpinResult.Actions[0].Count;
			var free = new WinFreeNum(this.context);
			free.setNumber(freeData);
			this.context.context.addChild(free);
			return;
		}
		
			// 		var free = new WinFreeNum(this.context);
			// free.setNumber(10);
			// 	this.context.context.addChild(free);

		if ((data.Value.Dollar >= 25 * (data.Value.SpinResult.Main.TotalBet/10000))&&this.sign) {
			//  */
			var win = new BigWin(this.context);
//			console.log("totalWin", data.Value.SpinResult.Main.TotalWin);
			win.setMoney(data.Value.SpinResult.Main.TotalWin  / 10000);
			this.context.context.addChild(win);
			return;
		}

		this.context.complete();
	}
	/**
	 * 整体界面完成，复原
	 */
	private completeSence(): void {
		var data = this.tdata;
		// this.context.crystalPanle.displayUserPanle();
		//设置总赢得和总投注
		this.context.context.crystalPanle.set_zhu_win(data.Value.SpinResult.Main.TotalBet  / 10000, data.Value.SpinResult.Main.TotalWin  / 10000);
		//设置当前的余额
		this.context.context.leftPanle.setMoney(data.Value.Balance);
		//设定免费次数
		if (data.Actions.freeslot) {
			this.context.context.freePanle.setFreeNum(data.Actions.freeslot.total, data.Actions.freeslot.count);
		}
		// this.context.context.freePanle.setFreeNum(10, 2);
	}

}